package cn.jeeweb.modules.client.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.excel.entity.params.ExcelExportEntity;
import cn.jeeweb.core.common.controller.BaseBeanController;
import cn.jeeweb.core.model.AjaxJson;
import cn.jeeweb.core.model.PageJson;
import cn.jeeweb.core.query.annotation.PageableDefaults;
import cn.jeeweb.core.query.data.PropertyPreFilterable;
import cn.jeeweb.core.query.data.Queryable;
import cn.jeeweb.core.query.data.Sort;
import cn.jeeweb.core.query.utils.QueryableConvertUtils;
import cn.jeeweb.core.query.wrapper.EntityWrapper;
import cn.jeeweb.core.security.shiro.authz.annotation.RequiresMethodPermissions;
import cn.jeeweb.core.security.shiro.authz.annotation.RequiresPathPermission;
import cn.jeeweb.core.utils.*;
import cn.jeeweb.modules.client.entity.*;
import cn.jeeweb.modules.client.service.IBaleService;
import cn.jeeweb.modules.client.service.IClientGroupBaleService;
import cn.jeeweb.modules.client.service.IClientOrderService;
import cn.jeeweb.modules.client.service.IGroupService;
import cn.jeeweb.modules.client.style.ExcelExportStatisticStyler;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializeFilter;
import com.google.gson.JsonObject;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.*;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.zip.ZipOutputStream;

/**
 * @Title: 订单
 * @Description: 订单
 * @author zhuht
 * @date 2018-08-09 20:21:54
 * @version V1.0
 *
 */
@Controller
@RequestMapping("${admin.url.prefix}/client/bale")
@RequiresPathPermission("client:bale")
public class BaleController extends BaseBeanController<ClientOrder> {

    @Autowired
    protected IClientOrderService orderService;
    @Autowired
    protected IBaleService baleService;
    @Autowired
    protected IGroupService groupService;
    @Autowired
    protected IClientGroupBaleService clientGroupBaleService;

    public ClientOrder get(String id) {
        if (!ObjectUtils.isNullOrEmpty(id)) {
            return orderService.selectById(id);
        } else {
            return newModel();
        }
    }

    @RequiresMethodPermissions("list")
    @RequestMapping(method = RequestMethod.GET)
    public String list(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("list");
    }

    @RequestMapping(value = "ajaxList", method = { RequestMethod.GET, RequestMethod.POST })
    @PageableDefaults(sort = "id=desc")
    private void ajaxList(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
        EntityWrapper<ClientOrder> entityWrapper = new EntityWrapper<ClientOrder>(entityClass);
        propertyPreFilterable.addQueryProperty("id");
        // 预处理
        String end = (String)queryable.getValue("createDate_end");
        if(end!=null){
            queryable.addCondition("createDate||le", end+" 24:00:00");
        }
        QueryableConvertUtils.convertQueryValueToEntityValue(queryable, entityClass);
        SerializeFilter filter = propertyPreFilterable.constructFilter(entityClass);
        PageJson<ClientOrder> pagejson = new PageJson<ClientOrder>(orderService.list(queryable,entityWrapper));
        String content = JSON.toJSONString(pagejson, filter);
        StringUtils.printJson(response, content);
    }


    @RequestMapping(value = "baleOrder", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson baleOrder() {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("打包成功");
        try {
            Map<String,Integer> num = new HashMap<String,Integer>(){{
                put("num",new Integer(1));
            }};
            while(true){
                boolean result = orderService.baleOrder(num);
                if(!result){
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("打包失败");
        }
        return ajaxJson;
    }

    @RequestMapping(value = "baleList", method = RequestMethod.GET)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response) {
        return display("baleList");
    }

    @RequestMapping(value = "ajaxListBale", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public void selectBalePage(Queryable queryable, HttpServletResponse response){
        PageJson<Map> pagejson = new PageJson<Map>(baleService.selectBalePage(queryable));
        String content = JSON.toJSONString(pagejson);
        StringUtils.printJson(response, content);
    }

    @RequestMapping(value = "groupOrder", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson groupOrder(HttpServletRequest request,String groups,String createDate) {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("分组成功");
        try {
            clientGroupBaleService.deleteByMap(new HashMap<String,Object>(){{put("create_date",createDate);}});
            //获取根路径
            ServletContext sc = request.getSession().getServletContext();
            String path = sc.getRealPath("/") + "temp";
            groups = Encodes.urlDecode(groups);
            JSONArray arry=JSON.parseArray(groups);
            int page = 0;
            for(Object obj:arry){
                JSONObject json = (JSONObject) obj;
                List<Map> bales = baleService.selectBaleAll(createDate,String.valueOf(page),json.get("size").toString());
                ClientGroupBale groupBale = new ClientGroupBale();
                groupBale.setGroupName(json.get("name").toString());
                groupBale.setGroupCount(json.get("size").toString());
                Double total = 0.0;

                List<Map> ordes = new ArrayList<Map>();
                for(Map b:bales){
                    List<Map> os = baleService.selectOrderList(b.get("order_id").toString());
                    Double sum = 0.0;

                    Map<String,Object> detail = new HashMap<String,Object>();
                    List<String> s = new ArrayList<String>();
                    for(Map o:os){
                        BigDecimal b1 = new BigDecimal(Double.toString(sum));
                        BigDecimal b2 = new BigDecimal(String.valueOf(o.get("price")));
                        sum = b1.add(b2).doubleValue();
                        o.put("img_url", sc.getRealPath("/") + o.get("img_url"));
                        String shopname = o.get("shop_name").toString().replaceAll(" ", "");
                        char fir = shopname.charAt(0);//获取字符串的第一个字符
                        s.add(String.valueOf(fir));
                    }

                    Map<String, Object> o = new HashMap<String, Object>();
                    o.put("number", "");
                    o.put("shop_name", "");
                    o.put("price", sum);
                    o.put("title", "");
                    o.put("img_url", "");
                    o.put("keyword", "");
                    o.put("keyword2", "");
                    o.put("keyword3", "");
                    o.put("keyword4", "");
                    o.put("keyword5", "");
                    o.put("offer_link", "");
                    o.put("remarks", "");

                    detail.put("list",os);
                    detail.put("number","C"+b.get("number"));
                    detail.put("sum",o);
                    detail.put("s",s);
                    ordes.add(detail);
                    BigDecimal b1 = new BigDecimal(Double.toString(total));
                    BigDecimal b2 = new BigDecimal(sum);
                    total = b1.add(b2).doubleValue();
                }
                groupBale.setGroupDetail(JSON.toJSONString(ordes));
                groupBale.setGroupTotal(String.valueOf(total));
                clientGroupBaleService.insert(groupBale);
                page = page+Integer.valueOf(json.get("size").toString());
            }

        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("分组失败");
        }
        return ajaxJson;
    }

    //导出单组
    @RequestMapping(value = "exportExc", method = { RequestMethod.GET, RequestMethod.POST })
    private void export(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,HttpServletResponse response,String createDate,String id) throws Exception {
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {
            ClientGroupBale clientGroupBale = clientGroupBaleService.selectById(id);

            String zipName = clientGroupBale.getGroupName()+ ".zip";
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, "UTF-8"));

            //获取根路径
            ServletContext sc = request.getSession().getServletContext();
            String path = sc.getRealPath("/") + "temp/";

            //任务汇总
            List<ExcelExportEntity> bale = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity excelentity = new ExcelExportEntity("任务编号", "number");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity(null, "orders");
            List<ExcelExportEntity> orders = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity shop_name = new ExcelExportEntity("店铺名称", "shop_name");
            shop_name.setWidth(50);
            orders.add(shop_name);
            ExcelExportEntity price = new ExcelExportEntity("客单价(元)", "price");
            orders.add(price);
            ExcelExportEntity title = new ExcelExportEntity("客单价备注", "title");
            title.setWidth(50);
            orders.add(title);
            ExcelExportEntity image = new ExcelExportEntity("主图", "img_url");
            image.setType(2);
            image.setExportImageType(1);
            image.setHeight(60);
            image.setWidth(30);
            orders.add(image);
            ExcelExportEntity keyword = new ExcelExportEntity("关键词1", "keyword");
            keyword.setWidth(50);
            orders.add(keyword);
            ExcelExportEntity offer_link = new ExcelExportEntity("筛选条件", "offer_link");
            offer_link.setWidth(50);
            orders.add(offer_link);
            excelentity.setList(orders);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("城市合伙人姓名", "remarks");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("本金", "bj");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("合计", "hj");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);

            //单个任务
            List<ExcelExportEntity> orde = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity number = new ExcelExportEntity("任务编号", "number");
            number.setMergeVertical(true);
            orde.add(number);
            ExcelExportEntity shops_name = new ExcelExportEntity("店铺名称", "shop_name");
            shops_name.setWidth(50);
            orde.add(shops_name);
            orde.add(new ExcelExportEntity("客单价(元)", "price"));
            ExcelExportEntity titles = new ExcelExportEntity("客单价备注", "title");
            titles.setWidth(50);
            orde.add(titles);
            ExcelExportEntity img = new ExcelExportEntity("主图", "img_url");
            img.setHeight(60);
            img.setWidth(30);
            img.setType(2);
            img.setExportImageType(1);
            orde.add(img);
            ExcelExportEntity keyword1 = new ExcelExportEntity("关键词1", "keyword");
            keyword1.setWidth(50);
            orde.add(keyword1);

            ExcelExportEntity keyword2 = new ExcelExportEntity("关键词2", "keyword2");
            keyword2.setWidth(50);
            orde.add(keyword2);

            ExcelExportEntity keyword3 = new ExcelExportEntity("关键词3", "keyword3");
            keyword3.setWidth(50);
            orde.add(keyword3);

            ExcelExportEntity keyword4 = new ExcelExportEntity("关键词4", "keyword4");
            keyword4.setWidth(50);
            orde.add(keyword4);

            ExcelExportEntity keyword5 = new ExcelExportEntity("关键词5", "keyword5");
            keyword5.setWidth(50);
            orde.add(keyword5);

            ExcelExportEntity offer_links = new ExcelExportEntity("筛选条件", "offer_link");
            offer_links.setWidth(50);
            orde.add(offer_links);
            orde.add(new ExcelExportEntity("城市合伙人姓名", "remarks"));

            JSONArray bales = JSON.parseArray(clientGroupBale.getGroupDetail());
            List<Map> orderList = new ArrayList<Map>();
            for(Object obj:bales){
                JSONObject map = (JSONObject) obj;
                JSONArray list = (JSONArray) map.get("list");
                String su = ((Map) map.get("sum")).get("price").toString();
                List list2 = new ArrayList();
                for(Object o:list){
                    JSONObject json = (JSONObject) o;
                    json.put("number", map.get("number").toString());
                    list2.add(json.clone());
                }
                Map m = new HashMap();
                m.put("number", map.get("number").toString());
                m.put("orders",list2);
                m.put("bj", su);
                orderList.add(m);


                list.add(map.get("sum"));
                List<String> s = (List<String>) map.get("s");
                ExportParams params = new ExportParams("", "", ExcelType.XSSF);
                params.setStyle(ExcelExportStatisticStyler.class);
                Workbook workbook = ExcelExportUtil.exportExcel(params, orde, list);
                String hzPath = path + map.get("number").toString()+"_"+su+ "_"+ org.apache.commons.lang3.StringUtils.join(s,"_")+"_"+ createDate+ ".xlsx";
                File file = new File(hzPath);
                FileOutputStream fos = new FileOutputStream(file);
                workbook.write(fos);
                fos.close();
                ZipUtils.doCompress(hzPath, out);
                file.delete();
            }


            ExportParams params = new ExportParams("任务汇总", "任务汇总", ExcelType.XSSF);
            params.setStyle(ExcelExportStatisticStyler.class);
            Workbook workbook = ExcelExportUtil.exportExcel(params, bale, orderList);
            String hzPath = "";
            hzPath = path + "任务汇总-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) +"-"+clientGroupBale.getGroupName()+ ".xlsx";
            File file = new File(hzPath);
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            fos.close();
            ZipUtils.doCompress(hzPath, out);
            file.delete();
            response.flushBuffer();

        } catch (Exception e) {
            logger.error("单个订单-excel下载异常",e);
            throw new RuntimeException("分配订单-excel下载异常",e);
        } finally {
            out.close();
        }
    }

    //导出单组
    @RequestMapping(value = "exportExcAll", method = { RequestMethod.GET, RequestMethod.POST })
    private void exportAll(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,HttpServletResponse response,String createDate) throws Exception {
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {
            EntityWrapper<ClientGroupBale> entityWrapper = new EntityWrapper<ClientGroupBale>(ClientGroupBale.class);
            entityWrapper.eq("create_date",createDate);
            List<ClientGroupBale> clientGroupBales = clientGroupBaleService.listWithNoPage(queryable,entityWrapper);

            String zipName = "任务汇总.zip";
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, "UTF-8"));

            //获取根路径
            ServletContext sc = request.getSession().getServletContext();
            String path = sc.getRealPath("/") + "temp/";

            //任务汇总
            List<ExcelExportEntity> bale = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity excelentity = new ExcelExportEntity("任务编号", "number");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity(null, "orders");
            List<ExcelExportEntity> orders = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity shop_name = new ExcelExportEntity("店铺名称", "shop_name");
            shop_name.setWidth(50);
            orders.add(shop_name);
            ExcelExportEntity price = new ExcelExportEntity("客单价(元)", "price");
            orders.add(price);
            ExcelExportEntity title = new ExcelExportEntity("客单价备注", "title");
            title.setWidth(50);
            orders.add(title);
            ExcelExportEntity image = new ExcelExportEntity("主图", "img_url");
            image.setType(2);
            image.setExportImageType(1);
            image.setHeight(60);
            image.setWidth(30);
            orders.add(image);
            ExcelExportEntity keyword = new ExcelExportEntity("关键词1", "keyword");
            keyword.setWidth(50);
            orders.add(keyword);
            ExcelExportEntity offer_link = new ExcelExportEntity("筛选条件", "offer_link");
            offer_link.setWidth(50);
            orders.add(offer_link);
            excelentity.setList(orders);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("城市合伙人姓名", "remarks");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("本金", "bj");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            excelentity = new ExcelExportEntity("合计", "hj");
            excelentity.setNeedMerge(true);
            bale.add(excelentity);
            List<Map> orderList = new ArrayList<Map>();
            for(ClientGroupBale clientGroupBale :clientGroupBales){
                JSONArray bales = JSON.parseArray(clientGroupBale.getGroupDetail());
                for(Object obj:bales){
                    JSONObject map = (JSONObject) obj;
                    JSONArray list = (JSONArray) map.get("list");
                    String su = ((Map) map.get("sum")).get("price").toString();
                    List list2 = new ArrayList();
                    for(Object o:list){
                        JSONObject json = (JSONObject) o;
                        list2.add(json.clone());
                    }
                    Map m = new HashMap();
                    m.put("number", map.get("number").toString());
                    m.put("orders",list2);
                    m.put("bj", su);
                    m.put("remarks",clientGroupBale.getGroupName());
                    orderList.add(m);
                }
            }

            if(orderList.size()>200){
                PagingList<Map> paging = new PagingList<Map>(orderList, 100);
                while (paging.hasNext()) {
                    List<Map> l = paging.next();
                    ExportParams params = new ExportParams("任务汇总", "任务汇总", ExcelType.XSSF);
                    params.setStyle(ExcelExportStatisticStyler.class);
                    Workbook workbook = ExcelExportUtil.exportExcel(params, bale, l);
                    String hzPath = "";
                    hzPath = path + "任务汇总-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) +"-"+paging.getCurPageNo()+ ".xlsx";

                    File file = new File(hzPath);
                    FileOutputStream fos = new FileOutputStream(file);
                    workbook.write(fos);
                    fos.close();
                    ZipUtils.doCompress(hzPath, out);
                    file.delete();
                }
            }else{
                ExportParams params = new ExportParams("任务汇总", "任务汇总", ExcelType.XSSF);
                params.setStyle(ExcelExportStatisticStyler.class);
                Workbook workbook = ExcelExportUtil.exportExcel(params, bale, orderList);
                String hzPath = "";
                hzPath = path + "任务汇总-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xlsx";
                File file = new File(hzPath);
                FileOutputStream fos = new FileOutputStream(file);
                workbook.write(fos);
                fos.close();
                ZipUtils.doCompress(hzPath, out);
                file.delete();
            }

        } catch (Exception e) {
            logger.error("汇总订单-excel下载异常",e);
            throw new RuntimeException("分配订单-excel下载异常",e);
        } finally {
            out.close();
        }
    }

    //统计下载
    @RequestMapping(value = "exportExcPool", method = { RequestMethod.GET, RequestMethod.POST })
    private void exportExcPool(Queryable queryable, PropertyPreFilterable propertyPreFilterable, HttpServletRequest request,HttpServletResponse response,String createDate) throws Exception {
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try {
            EntityWrapper<ClientGroupBale> entityWrapper = new EntityWrapper<ClientGroupBale>(ClientGroupBale.class);
            entityWrapper.eq("create_date",createDate);
            List<ClientGroupBale> clientGroupBales = clientGroupBaleService.listWithNoPage(queryable,entityWrapper);

            //获取根路径
            ServletContext sc = request.getSession().getServletContext();
            String path = sc.getRealPath("/") + "temp/";

            String zipName = "统计.zip";
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, "UTF-8"));

            //任务汇总
            List<ExcelExportEntity> bale = new ArrayList<ExcelExportEntity>();
            bale.add(new ExcelExportEntity("名称", "group"));
            bale.add(new ExcelExportEntity("数量", "count"));
            bale.add(new ExcelExportEntity("金额", "total"));

            List<Map> orderList = new ArrayList<Map>();
            for(ClientGroupBale clientGroupBale :clientGroupBales){
                Map m = new HashMap();
                m.put("group", clientGroupBale.getGroupName());
                m.put("count",clientGroupBale.getGroupCount());
                m.put("total", clientGroupBale.getGroupTotal());
                orderList.add(m);
            }

            ExportParams params = new ExportParams("统计", "统计", ExcelType.XSSF);
            params.setStyle(ExcelExportStatisticStyler.class);
            Workbook workbook = ExcelExportUtil.exportExcel(params, bale, orderList);
            String hzPath = "";
            hzPath = path + "统计-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xlsx";
            File file = new File(hzPath);
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            fos.close();
            ZipUtils.doCompress(hzPath, out);
            file.delete();
            response.flushBuffer();

        } catch (Exception e) {
            logger.error("汇总订单-excel下载异常",e);
            throw new RuntimeException("分配订单-excel下载异常",e);
        } finally {
            out.close();
        }
    }

    @RequestMapping(value = "group", method = RequestMethod.GET)
    public String create(Model model, HttpServletRequest request, HttpServletResponse response,Queryable queryable,String createDate) {
        EntityWrapper<Group> entityWrapper = new EntityWrapper<Group>(Group.class);
        model.addAttribute("groups", groupService.listWithNoPage(queryable,entityWrapper));
        model.addAttribute("total",orderService.getMaxNumber(createDate));
        model.addAttribute("createDate",createDate);
        return display("group");
    }

    @RequestMapping(value = "selectOrderByBaleState", method = { RequestMethod.GET, RequestMethod.POST })
    public void selectOrderByBaleState(HttpServletRequest request,HttpServletResponse response,String createDate,String createDateEnd) throws Exception {
        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        try{
            String zipName = "未分配订单.zip";
            response.setContentType("APPLICATION/OCTET-STREAM");
            response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(zipName, "UTF-8"));
            //单个任务
            List<ExcelExportEntity> orde = new ArrayList<ExcelExportEntity>();
            ExcelExportEntity shops_name = new ExcelExportEntity("店铺名称", "shop_name");
            shops_name.setWidth(50);
            orde.add(shops_name);
            orde.add(new ExcelExportEntity("客单价(元)", "price"));
            ExcelExportEntity titles = new ExcelExportEntity("客单价备注", "title");
            titles.setWidth(50);
            orde.add(titles);
            ExcelExportEntity img = new ExcelExportEntity("主图", "img_url");
            img.setHeight(60);
            img.setWidth(30);
            img.setType(2);
            img.setExportImageType(1);
            orde.add(img);

            ExcelExportEntity keyword = new ExcelExportEntity("关键词1", "keyword");
            keyword.setWidth(50);
            orde.add(keyword);

            ExcelExportEntity keyword2 = new ExcelExportEntity("关键词2", "keyword2");
            keyword2.setWidth(50);
            orde.add(keyword2);

            ExcelExportEntity keyword3 = new ExcelExportEntity("关键词3", "keyword3");
            keyword3.setWidth(50);
            orde.add(keyword3);

            ExcelExportEntity keyword4 = new ExcelExportEntity("关键词4", "keyword4");
            keyword4.setWidth(50);
            orde.add(keyword4);

            ExcelExportEntity keyword5 = new ExcelExportEntity("关键词5", "keyword5");
            keyword5.setWidth(50);
            orde.add(keyword5);
            ExcelExportEntity offer_links = new ExcelExportEntity("筛选条件", "offer_link");
            offer_links.setWidth(50);
            orde.add(offer_links);
            orde.add(new ExcelExportEntity("城市合伙人姓名", "remarks"));

            //获取根路径
            ServletContext sc = request.getSession().getServletContext();
            String path = sc.getRealPath("/") + "temp/";

            List<Map> os = baleService.selectOrderByBaleState(createDate,createDateEnd);
            for(Map o:os){
                o.put("img_url", sc.getRealPath("/") + o.get("img_url"));
            }

            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("未分配订单", "未分配订单"), orde, os);
            String hzPath = path + "未分配订单-" + new SimpleDateFormat("yyyyMMddhhmmss").format(new Date()) + ".xlsx";
            File file = new File(hzPath);
            FileOutputStream fos = new FileOutputStream(file);
            workbook.write(fos);
            fos.close();
            ZipUtils.doCompress(hzPath, out);
            response.flushBuffer();
            file.delete();
        } catch (Exception e) {
            logger.error("未分配订单-excel下载异常",e);
            throw new RuntimeException("未分配订单-excel下载异常",e);
        } finally {
            out.close();

        }
    }

    @RequestMapping(value = "delete", method = { RequestMethod.GET, RequestMethod.POST })
    @ResponseBody
    public AjaxJson delete() {
        AjaxJson ajaxJson = new AjaxJson();
        ajaxJson.success("删除成功");
        try {
            orderService.delete(null);
            baleService.delete(null);
        } catch (Exception e) {
            e.printStackTrace();
            ajaxJson.fail("删除失败");
        }
        return ajaxJson;
    }
}
